package com.tqjc.common.ScheduledTask;

import cn.hutool.core.collection.CollectionUtil;
import com.tqjc.common.consts.NormalConst;
import com.tqjc.common.faulthandle.entity.BO.SemiAutoTaskCacheBO;
import com.tqjc.execute.thread.aysn.AysnSemiAutoTaskSaveThread;
import com.tqjc.execute.thread.aysn.AysnSemiAutoTaskUpdateThread;
import com.tqjc.execute.utils.HttpPLCUtils;
import com.tqjc.mes.entity.SteelInfo;
import com.tqjc.mes.entity.Test;
import com.tqjc.mes.service.SteelInfoService;
import com.tqjc.mes.service.TestService;
import com.tqjc.system.core.threadpool.ThreadPoolManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author: 86195
 * @CreateTime: 2024-06-06  15:33
 * @Description: 半自动任务记录
 * @Version: 1.0
 */
@Slf4j
@Component
public class SemiAutoTask {

    @Autowired
    SteelInfoService steelInfoService;

    @Value("${server_host}")
    private String serverHost;

    @Autowired
    private RestTemplate restTemplate;

    @Resource
    TestService testService;

    private SemiAutoTaskCacheBO car2 = new SemiAutoTaskCacheBO();

    private SemiAutoTaskCacheBO car3 = new SemiAutoTaskCacheBO();

    private SemiAutoTaskCacheBO car4 = new SemiAutoTaskCacheBO();

    private SemiAutoTaskCacheBO car5 = new SemiAutoTaskCacheBO();

    private SemiAutoTaskCacheBO carDJ2 = new SemiAutoTaskCacheBO();

    private SemiAutoTaskCacheBO carDJ3 = new SemiAutoTaskCacheBO();



    @Scheduled(cron = "0/3 * *  * * ? ")
    public void car3Listen() {
        runTask("3#横移", car3);
    }

    @Scheduled(cron = "0/3 * *  * * ? ")
    public void car4Listen() {
        runTask("4#横移", car3);
    }


    @Scheduled(cron = "0/3 * *  * * ? ")
    public void car5Listen() {
        runTask("5#横移", car3);
    }


    @Scheduled(cron = "0/3 * *  * * ? ")
    public void carDJ2Listen() {
        runTask("11C北", car3);
    }

    @Scheduled(cron = "0/3 * *  * * ? ")
    public void carDJ3Listen() {
        runTask("11C南", car3);
    }

    private void runTask(String crane, SemiAutoTaskCacheBO cacheBO) {
        try {
            //1.监听模式点位信息
            String mode = HttpPLCUtils.plcReadbc(restTemplate,serverHost, crane,"模式",false);
            if(!"3".equals(mode)){
                //非半自动模式下不需要监听
                return;
            }



            String flagStr = HttpPLCUtils.plcReadbc(restTemplate, serverHost, crane, "半自动吸料完成", false);

            if(flagStr.equalsIgnoreCase("true") && !flagStr.equalsIgnoreCase(cacheBO.getLastFlag())){
                //true 正在吸料 &&  且与上次不同    表示吸料动作开始
                //2.获取一级辊道上的钢板信息
                String positionFlag = HttpPLCUtils.plcReadbc(restTemplate, serverHost, crane, "行车在辊道位", false);//获取钢板号
                if(positionFlag.equalsIgnoreCase("true")){

                    Date startDate = new Date();
                    //行车确实在辊道位
                    String steelNoLabel = this.getSteelNoLabel(crane);
                    String steelNo = HttpPLCUtils.plcReadbc(restTemplate,serverHost, crane,steelNoLabel,true); //获取钢板号
                    if("11C南".equals(crane)||"11C北".equals(crane)){
                        if(StringUtils.isNotEmpty(steelNo)&&steelNo.length()==7){
                            steelNo="0"+steelNo;
                        }
                        if(StringUtils.isNotEmpty(steelNo)&&steelNo.length()==8){
                            steelNo = steelNo.substring(0,5)+"100";
                        }
                    }

                    //从MES数据库中找到钢板信息
                    List<SteelInfo> steels= steelInfoService.selectByMatNo(steelNo);
//                    List<SteelInfo> steels = testService.mockData(steelNo);
                    if(CollectionUtil.isNotEmpty(steels)){
                        //更新缓存数据对象的钢板号
                        cacheBO.setSteelNo(steels.get(0).getMatNo());
                        //更新缓存数据对象的吸具状态
                        cacheBO.setLastFlag(flagStr);
                        //保存数据
                        AysnSemiAutoTaskSaveThread saveThread = AysnSemiAutoTaskSaveThread.builder().craneType(crane)
                                .steelInfo(steels.get(0)).startDate(startDate).build();
                        ThreadPoolManager.getInstance().execute(saveThread);
                    }
                }
            }


            if(flagStr.equalsIgnoreCase("false") && !flagStr.equalsIgnoreCase(cacheBO.getLastFlag())){
                Date endDate = new Date();
                //false 正在放料  &&  且与上次不同    表示放料动作开始
                //2.获取一级辊道上的钢板信息
                String positionFlag = HttpPLCUtils.plcReadbc(restTemplate, serverHost, crane, "行车在垛位1", false);//获取钢板号
                if(positionFlag.equalsIgnoreCase("true")){

                    AysnSemiAutoTaskUpdateThread autoTaskUpdateThread = AysnSemiAutoTaskUpdateThread.builder()
                            .endTime(endDate).stockNo("1").steelNo(cacheBO.getSteelNo()).craneName(crane).build();
                    ThreadPoolManager.getInstance().execute(autoTaskUpdateThread);
                    cacheBO.setLastFlag(flagStr);
                } else {
                    positionFlag = HttpPLCUtils.plcReadbc(restTemplate, serverHost, crane, "行车在垛位2", false);//获取钢板号
                    if(positionFlag.equalsIgnoreCase("true")){
                        AysnSemiAutoTaskUpdateThread autoTaskUpdateThread = AysnSemiAutoTaskUpdateThread.builder()
                                .endTime(endDate).stockNo("2").craneName(crane).steelNo(cacheBO.getSteelNo()).build();
                        ThreadPoolManager.getInstance().execute(autoTaskUpdateThread);
                        cacheBO.setLastFlag(flagStr);
                    }

                }

            }



        } catch (Exception e){
            log.info("天车【{}】监听半自动任务数据出现异常", crane,e);
        }
    }

    private String getSteelNoLabel(String crane) {
        if("2#横移".equals(crane)){
            return "上料钢板号";
        }  else if("4#横移".equals(crane)){
            return "4#横移下线钢板号";
        } else if("5#横移".equals(crane)){
            return "5#横移下线钢板号";
        } else {
            return "获取钢板号";
        }
    }


}
